#include "InPerSteppingMotor.h"

#define DEF_TAG         "StepMotor"

void Ext_InPerStepMotor_Up(void)
{
    Ext_SdkMotor_Up(SDK_MOTOR_1);
}

void Ext_InPerStepMotor_Down(void)
{
    Ext_SdkMotor_Down(SDK_MOTOR_1);
}

void Ext_InPerStepMotor_TurnLeft(void)
{
#if CMAKE_STEP_MOTOR_REVERSE
    Ext_SdkMotor_TurnRight(SDK_MOTOR_0);
#else
    Ext_SdkMotor_TurnLeft(SDK_MOTOR_0);
#endif
}

void Ext_InPerStepMotor_TurnRight(void)
{
#if CMAKE_STEP_MOTOR_REVERSE
    Ext_SdkMotor_TurnLeft(SDK_MOTOR_0);
#else
    Ext_SdkMotor_TurnRight(SDK_MOTOR_0);
#endif
}

Com_pvoid InPer_StepMotor_Thread(Com_pvoid arg)
{
    int motor_num = (int)arg;

    if(Ext_InComFile_Access(DEF_FLAG_NO_NETWORK) == DEF_COM_FILE_EXIST)
    {
        switch(motor_num)
        {
        case SDK_MOTOR_0 :
        {
            Ext_SdkMotor_Init_Center(SDK_MOTOR_0);
        }
            break;

        case SDK_MOTOR_1 :
        {
            Ext_SdkMotor_Init_Center(SDK_MOTOR_1);
        }
            break;

        default:
            break;
        }
    }

    return NULL;
}

void Ext_InPerStepMotor_Init(void)
{
    Ext_SdkMotor_Init();

    pthread_t id;
    pthread_create(&id,NULL,InPer_StepMotor_Thread,(void *)SDK_MOTOR_0);
    pthread_detach(id);

#if CMAKE_SUPPORT_PTZ
    pthread_t id_o;
    pthread_create(&id_o,NULL,InPer_StepMotor_Thread,(void *)SDK_MOTOR_1);
    pthread_detach(id_o);
#endif
}

void Ext_InPerStepMotor_Destroy(void)
{
    Ext_SdkMotor_Destroy();
}
